﻿function getParams() {

    var params = {};

    var companyName = $("input[name='companyName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(companyName)) {
        params.companyName = companyName;
    }

    var bankName = $("input[name='bankName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(bankName)) {
        params.bankName = bankName;
    }

    var cardNumber = $("input[name='cardNumber']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(cardNumber)) {
        params.cardNumber = cardNumber;
    }

    var cardName = $("input[name='cardName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(cardName)) {
        params.cardName = cardName;
    }

    var enableStatus = $("select[name='enableStatus']").find("option:selected").val();
    if (!FlashPay.Util.isNullOrEmptySpance(enableStatus)) {
        params.enableStatus = enableStatus;
    }

    var lastUpdateDateSortType = $("select[name='lastUpdateDateSortType']").find("option:selected").val();
    if (!FlashPay.Util.isNullOrEmptySpance(lastUpdateDateSortType)) {
        params.lastUpdateDateSortType = lastUpdateDateSortType;
    }

    return params;
}

function replace_field(key) {

    var data = {
        "公司名称": "Company Name",
        "银行名称": "Bank Name",
        "银行卡号": "Card Number",
        "类型": "Card Type",
        "用户名": "Username",
        "使用状态": "Status",
        "启用状态": "Enable Status",
        "成功笔数 / 金额": "Quantity/Amount",
        "参考余额": "Balance",
        "收款费率(%)": "Rates (%)",
        "更新时间": "Update Time",
        "虚拟机编码": "Virtual Machine",
        "备注": "Remarks",
        "操作": "Operate"
    };

    var language = FlashPay.Util.Language();

    if (!FlashPay.Util.isNullOrEmptySpance(language) && language == "en") {
        return data[key];
    } else {
        return key;
    }
}

function doSearch() {

    var columns = [[
        { field: 'companyName', title: replace_field('公司名称'), width: 100, align: 'left' },
        {
            field: 'bankName',
            title: replace_field('银行名称'),
            width: 100,
            align: 'left',
            formatter: function (val, row) {
                return "{0}".format(val);
            }
        },
        {
            field: 'cardNumber',
            title: replace_field('银行卡号'),
            width: 135,
            align: 'left',
            formatter: function (val, row) {
                return val;
            }
        },
        {
            field: 'cardType',
            title: replace_field('类型'),
            width: 100,
            align: 'left',
            formatter: function (val, row) {
                return FlashPay.Util.GetCardTypeValue(val);
            }
        },
        { field: 'cardName', title: replace_field('用户名'), width: 100, align: 'left' },
        {
            field: 'usingStatus',
            title: replace_field('使用状态'),
            width: 65,
            align: 'center',
            formatter: function (val, row) {
                var result = "";
                if (val == 1) {
                    result = '<span class="green bold">有效</span>';
                } else {
                    result = '<span class="red bold">冻结</span>';
                }
                return result;
            }
        },
        {
            field: 'enableStatus',
            title: replace_field('启用状态'),
            width: 65,
            align: 'center',
            formatter: function (val, row) {

                var result = "";

                switch (val) {
                    case 1:
                        result = " <span class='green bold'>启用</span> ";
                        break;
                    case 2:
                        result = " <span class='red bold'>禁用</span> ";
                        break;
                    case 3:
                        result = "删除";
                        break;
                    case 4:
                        result = " <span class='#1C1C1C bold'>未授权</span> ";
                        break;
                }

                return result;
            }
        },
        {
            field: 'quantity', title: replace_field('成功笔数 / 金额'), width: 100, align: 'left',
            formatter: function (val, row) {
                return val + " / " + row.depositAmount;
            },
            styler: function (value, item, index) {
                if (item.limit) {
                    return 'background-color:#f0ad4e;color:white;';
                }
            }
        },
        {
            field: 'balance',
            title: replace_field('参考余额'),
            width: 100,
            align: 'right'
        },
        {
            field: 'depositFeeRatio', title: replace_field('收款费率(%)'), width: 100, align: 'right',
            formatter: function (val, row) {
                return FlashPay.Util.FormatNumber(val * 100, 2);
            }
        },
        {
            field: 'balanceLastUpdate',
            title: replace_field('更新时间'),
            width: 120,
            align: 'left',
            styler: function (value, item, index) {
                if (item.timeDifference >= 3 && item.enableStatus == 1) {
                    return 'background-color:red;color:white;';
                }
            }
        },
        { field: 'vmClientId', title: replace_field('虚拟机编码'), width: 120, align: 'left' },
        {
            field: 'remark',
            title: replace_field('备注'),
            width: 250,
            align: 'left'
        },
        {
            field: 'bcId', title: replace_field('操作'), width: 150, align: 'left',
            formatter: function (val, item) {
                var result = "";

                if (item.enableStatus == 1) {
                    result = '<a class="btn btn-warning btn-xs" data-confirm="{0} <br>确定要<span class=orange>禁用</span>该银行卡？" data-href="/Receipt/ReceiptCardUpdateStatus?id={1}&status=2">禁用</a>'.format(item.cardName, val);
                } else {
                    result += '<a class="btn btn-success btn-xs" data-confirm="{0} <br>确定要<span class=orange>启用</span>该银行卡？" data-href="/Receipt/ReceiptCardUpdateStatus?id={1}&status=1">启用</a>'.format(item.cardName, val);
                    result += ' <button type="button" class="btn btn-default btn-xs" data-modal="/view/receipt/receiptcardEdit.html?id={0}" data-title="编辑 - {1}-{2}-{3}" data-width="452" data-height="120"><span class="glyphicon glyphicon-edit"></span> 编辑</button>'.format(val, item.bankName, item.cardName, FlashPay.Util.SubCardNumber(item.cardNumber));
                }

                return result;
            }
        }
    ]];

    $("#receiptcard").datagrid({
        fit: false,
        loadMsg: '数据加载中，请稍后……',
        url: '/Receipt/ReceiptCardQuery',
        queryParams: getParams(),
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        pageSize: pageSize,
        pageList: [15, 30, 50, 100, 1000],
        height: $(window).height() - 70,
        idField: 'bcId',
        onLoadSuccess: function () {
            pageSize = $('#receiptcard').datagrid('options').pageSize;
        },
        columns: columns
    });
}